﻿using System;
using Lucas.Wendell.PI.Model;
using Lucas.Wendell.PI.Model.Enum;
using Senac.NetMob.Models;

namespace Lucas.Wendell.PI.Service
{
    public abstract class BaseService
    {
        public void Save(BaseModel model)
        {
            try
            {
                Validate(model);

                if (model.Id > 0)
                {
                    Update(model);
                }
                else
                {
                    model.Ativo = true;
                    Insert(model);
                }
            }
            catch (NetImobiliariaException)
            {
                throw;
            }
            catch (Exception e)
            {
                throw new NetImobiliariaException(NetImobiliariaExceptionTypes.UnexpectedError,
                                                  NetImobiliariaConstants.ERROR_CREATING_UPDATING, e);
            }
        }

        protected abstract bool Insert(BaseModel model);

        protected abstract bool Update(BaseModel model);

        public abstract bool Delete(BaseModel model);

        protected abstract void Validate(BaseModel model);
    }
}
